package com.cssy.xxkj.logic.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicMaterialBackDAO;
import com.cssy.xxkj.logic.dao.LogicMaterialBackItemDAO;
import com.cssy.xxkj.logic.dao.LogicProductionStorageinItemDAO;
import com.cssy.xxkj.logic.dao.LogicProductionStorageoutItemDAO;
import com.cssy.xxkj.logic.dao.LogicPurchaseWareItemDAO;
import com.cssy.xxkj.logic.dao.LogicSalesDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseBatchInItemDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseMaterialoutItemDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseOtherinItemDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseOtheroutItemDAO;
import com.cssy.xxkj.logic.entity.LogicMaterialBackDO;
import com.cssy.xxkj.logic.entity.LogicMaterialBackItemDO;
import com.cssy.xxkj.logic.entity.LogicProductionStorageinDO;
import com.cssy.xxkj.logic.entity.LogicProductionStorageinItemDO;
import com.cssy.xxkj.logic.entity.LogicProductionStorageoutDO;
import com.cssy.xxkj.logic.entity.LogicProductionStorageoutItemDO;
import com.cssy.xxkj.logic.entity.LogicPurchaseWareDO;
import com.cssy.xxkj.logic.entity.LogicPurchaseWareItemDO;
import com.cssy.xxkj.logic.entity.LogicSalesDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseBatchInDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseBatchInItemDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseMaterialoutItemDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtherinDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtherinItemDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseOtheroutItemDO;
import com.cssy.xxkj.logic.service.ILogicHsUtilService;
import com.cssy.xxkj.logic.service.ILogicMaterialBackService;
import com.cssy.xxkj.logic.service.ILogicProductionStorageInService;
import com.cssy.xxkj.logic.service.ILogicProductionStorageOutService;
import com.cssy.xxkj.logic.service.ILogicPurchaseWareService;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherInService;
import com.cssy.xxkj.logic.service.ILogicWareHouseOtherOutService;
import com.cssy.xxkj.logic.service.ILogicWarehouseBatchInService;
import com.cssy.xxkj.logic.service.IMaterialOutService;
import com.cssy.xxkj.logic.web.SalesController;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicMaterialBackService")
public class LogicMaterialServiceImpl implements ILogicMaterialBackService {
	
	@Resource(name = "logicMaterialBackDAO")
	private LogicMaterialBackDAO logicMaterialBackDAO;
	
	@Resource(name = "logicSalesDAO")
	private LogicSalesDAO logicSalesDAO;
	
	@Resource(name = "logicMaterialBackItemDAO")
	private LogicMaterialBackItemDAO logicMaterialBackItemDAO;
	//采购入库
	@Resource(name = "logicPurchaseWareService")
	private ILogicPurchaseWareService logicPurchaseWareService;
	@Resource(name = "logicPurchaseWareItemDAO")
	private LogicPurchaseWareItemDAO logicPurchaseWareItemDAO;
	//其他入库
	@Resource(name = "logicWareHouseOtherInService")
	private ILogicWareHouseOtherInService logicWareHouseOtherInService;
	@Resource(name = "logicWarehouseOtherinItemDAO")
	private LogicWarehouseOtherinItemDAO logicWarehouseOtherinItemDAO;
	//其他出库
	@Resource(name = "logicWareHouseOtherOutService")
	private ILogicWareHouseOtherOutService logicWareHouseOtherOutService;
	@Resource(name = "logicWarehouseOtheroutItemDAO")
	private LogicWarehouseOtheroutItemDAO logicWarehouseOtheroutItemDAO;
	//领料出库
	@Resource(name="materialOutServiceImpl")
	private IMaterialOutService materialOutService;
	@Resource(name="logicWarehouseMaterialoutItemDAO")
	private LogicWarehouseMaterialoutItemDAO logicWarehouseMaterialoutItemDAO;
	//成品入库
	@Resource(name = "logicProductionStorageInService")
	private ILogicProductionStorageInService logicProductionStorageInService;
	@Resource(name = "logicProductionStorageinItemDAO")
	private LogicProductionStorageinItemDAO logicProductionStorageinItemDAO;
	//成品出库
	@Resource(name = "logicProductionStorageOutService")
	private ILogicProductionStorageOutService logicProductionStorageOutService;
	@Resource(name = "logicProductionStorageoutItemDAO")
	private LogicProductionStorageoutItemDAO logicProductionStorageoutItemDAO;
	
	@Resource(name = "logicHsUtilService")
	private ILogicHsUtilService logicHsUtilService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicWarehouseBatchInService")
	private ILogicWarehouseBatchInService logicWarehouseBatchInService;
	
	@Resource(name = "logicWarehouseBatchInItemDAO")
	private LogicWarehouseBatchInItemDAO logicWarehouseBatchInItemDAO;

	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs = new SearchResult(scs);
		List<LogicMaterialBackDO> list = logicMaterialBackDAO.find(scs);
		rs.setRecordCount(logicMaterialBackDAO.count(scs));
		rs.setResults(list);
		return rs;
	}
	
	@Override
	public List<LogicMaterialBackItemDO> findItemListByMap(Map<String, Object> paramMap)
			throws SyBaseException{
		return logicMaterialBackItemDAO.findListByMap(paramMap);
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return logicMaterialBackDAO.deleteLogicMaterialBackDOByPrimaryKey(id);
	}

	@Override
	public Integer update(LogicMaterialBackDO curAp, User user)
			throws SyBaseException {
		curAp.setBizModifyName(user.getName());
		curAp.setBizModifyDate(new Date());
		curAp.setBizVersionMark(curAp.getBizVersion());
		curAp.setBizVersion(curAp.getBizVersion()+Integer.valueOf(1));
		return logicMaterialBackDAO.updateLogicMaterialBackDO(curAp);
	}

	@Override
	public LogicMaterialBackDO save(LogicMaterialBackDO curAp, User user)
			throws SyBaseException {
		curAp.setBizVersion(Integer.valueOf(1));
		curAp.setBizCreatorName(user.getName());
		curAp.setBizCreatorDate(new Date());
		logicMaterialBackDAO.insertLogicMaterialBackDO(curAp);
		return curAp;
	}
	
	@Override
	public void materialBackSave(HttpServletRequest request, LogicMaterialBackDO curAp)
			throws SyBaseException{
		try{
			String flagMark = request.getParameter("flagMark");
			String zd004Date = request.getParameter("zd004Date");
			String itemdata = request.getParameter("itemdata");//新增数据
			String itemdete = request.getParameter("itemdete");
			if(StringUtils.isNotBlank(zd004Date)){
				curAp.setZd004Date(DateUtils.stringToDate(zd004Date));
			}
			String zd004EntryDate = request.getParameter("zd004EntryDate");
			if(StringUtils.isNotBlank(zd004EntryDate)){
				curAp.setZd004EntryDate(DateUtils.stringToDate(zd004EntryDate));
			}
			if(BillStatusUtil.MATER_BACK_TWO.equals(flagMark)){
				//驳回
				LogicMaterialBackDO gmckEntity = logicMaterialBackDAO.findLogicMaterialBackDOByPrimaryKey(curAp.getId());
				if(gmckEntity==null){
					throw new SyBaseException("您修改的行数据发生改变，请重新点开查看");
				}
				gmckEntity.setZd003Statue(BillStatusUtil.MATER_BACK_TWO);
				this.update(gmckEntity, SessionUtil.getUser(request));
			}else{
				List<LogicMaterialBackItemDO> backItemList = JSONArray.parseArray(itemdata,LogicMaterialBackItemDO.class);
				if(curAp==null||StringUtils.isBlank(curAp.getId())){
					//新增
					String backId = IDCodeTypeUtil.getId();
					curAp.setId(backId);
					String backCode = syCodeService.getCode(BillTypeConstant.MATERBACK,BillTypeConstant.MATERBACK,SyCode.DATE_YYMM,5);
					curAp.setZd001Code(backCode);
					curAp.setZd003Statue(flagMark);
					this.save(curAp, SessionUtil.getUser(request));
					for(LogicMaterialBackItemDO backItem : backItemList){
						if(backItem==null){
							continue;
						}
						backItem.setId(IDCodeTypeUtil.getId());
						backItem.setZd001BackId(backId);
						backItem.setZd001BackCode(backCode);
						logicMaterialBackItemDAO.insertLogicMaterialBackItemDO(backItem);
					}
				}else{
					//修改
					if(BillStatusUtil.MATER_BACK_THREE.equals(flagMark)){
						//审核通过
						LogicMaterialBackDO gmckEntity = logicMaterialBackDAO.findLogicMaterialBackDOByPrimaryKey(curAp.getId());
						if(gmckEntity==null){
							throw new SyBaseException("数据发生改变，请重新提交");
						}
						String billType = curAp.getZd002Type();
						Map<String, BigDecimal> hasBackMap = this.findHsBackQuantityToMap(curAp.getZd005OriId());
						Map<String, Object> billBackMap = this.findQueryBackBillToMap(curAp.getZd005OriId(), billType);
						for(LogicMaterialBackItemDO backItem : backItemList){
							if(backItem==null||StringUtils.isBlank(backItem.getId())){
								continue;
							}
							//原ID 现在的ID 型号  规格  颜色 材质
							String keyStr = curAp.getZd005OriId()+"_"+backItem.getZd002MaterId();
							keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterModel())?null:backItem.getZd002MaterModel());
							keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterSpec())?null:backItem.getZd002MaterSpec());
							keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterColor())?null:backItem.getZd002MaterColor());
							keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterQuanlity())?null:backItem.getZd002MaterQuanlity());
							keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd012PiCode())?null:backItem.getZd012PiCode());
							
							//本次退回数量
							BigDecimal bTheQuantity = backItem.getZd010TheBackQuantity()==null?BigDecimal.ZERO:backItem.getZd010TheBackQuantity();
							//入库数量
							BigDecimal sgQuantity = backItem.getZd007SgQuantity()==null?BigDecimal.ZERO:backItem.getZd007SgQuantity();
							//总退回数量
							BigDecimal sbackQuantity = BigDecimal.ZERO;
							if(hasBackMap==null||hasBackMap.get(keyStr)==null){
								sbackQuantity = bTheQuantity;
							}else{
								sbackQuantity = bTheQuantity.add(hasBackMap.get(keyStr));
							}
							int compUp = sbackQuantity.compareTo(sgQuantity);
							if(compUp==1){
								throw new SyBaseException("总退货数量大于入库数量");
							}
							//修改原始单据的item的已退货数量
							processBackBillBackQuantity(billBackMap, billType, keyStr, bTheQuantity, SessionUtil.getUser(request));
							//修改库存
							logicHsUtilService.processHsMaterialBackItem(curAp, modifyStorage(billType, backItem), SessionUtil.getUser(request));
							
						}
						//更新主表信息
						if(StringUtils.isNotBlank(zd004Date)){
							gmckEntity.setZd004Date(DateUtils.stringToDate(zd004Date));
						}
						if(StringUtils.isNotBlank(zd004EntryDate)){
							gmckEntity.setZd004EntryDate(DateUtils.stringToDate(zd004EntryDate));
						}
						gmckEntity.setZd003Statue(flagMark);
						this.update(gmckEntity, SessionUtil.getUser(request));
					}else{
						curAp.setZd003Statue(flagMark);
						Integer rowUp = this.update(curAp, SessionUtil.getUser(request));
						if(rowUp==null || rowUp.intValue()==0){
							throw new SyBaseException("数据发生改变，请重新提交。");
						}else{
							for(LogicMaterialBackItemDO backItem : backItemList){
								if(backItem==null){
									continue;
								}
								if(StringUtils.isBlank(backItem.getId())){
									backItem.setId(IDCodeTypeUtil.getId());
									backItem.setZd001BackId(curAp.getId());
									backItem.setZd001BackCode(curAp.getZd001Code());
									logicMaterialBackItemDAO.insertLogicMaterialBackItemDO(backItem);
								}else{
									logicMaterialBackItemDAO.updateLogicMaterialBackItemDO(backItem);
								}
							}
						}
					}
					//删除不需要的
					if(StringUtils.isNotBlank(itemdete)){
						List<LogicMaterialBackItemDO> _backItemList = JSONArray.parseArray(itemdete,LogicMaterialBackItemDO.class);
						for(LogicMaterialBackItemDO _backItem : _backItemList){
							if(_backItem==null || StringUtils.isBlank(_backItem.getId())){
								continue;
							}
							logicMaterialBackItemDAO.deleteLogicMaterialBackItemDOByPrimaryKey(_backItem.getId());
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
	}
	private  LogicMaterialBackItemDO modifyStorage( String billType,LogicMaterialBackItemDO backItem){
		if("PCSW".equals(billType)||"GWHN".equals(billType)||"GPSI".equals(billType)||"BI".equals(billType)){//采购入库//其他入库 //成品入库//多PI入库
			backItem.setZd010TheBackQuantity(BigDecimal.ZERO.subtract(backItem.getZd010TheBackQuantity()));
		}
		return backItem;
	} 
	
	/**
	 * 更改原始单据行记录的item的退回数量
	 * @param billMap
	 * @param zdBillType
	 * @param keyStr
	 * @param backQuantity
	 * @throws SyBaseException
	 */
	private void processBackBillBackQuantity(Map<String, Object> billMap,
			String zdBillType, String keyStr, BigDecimal backQuantity,User user)
			throws SyBaseException {
		try{
			if(billMap==null||StringUtils.isBlank(zdBillType)||StringUtils.isBlank(keyStr)){
				return;
			}
			//采购入库
			if("PCSW".equals(zdBillType)){
				LogicPurchaseWareItemDO pcswItem = (LogicPurchaseWareItemDO)billMap.get(keyStr);
				if(pcswItem==null||StringUtils.isBlank(pcswItem.getId())){
					throw new SyBaseException("没有找到对应的入库单的行记录信息");
				}
				BigDecimal hasBackQuantity = pcswItem.getZd020BackQuantity()==null?BigDecimal.ZERO:pcswItem.getZd020BackQuantity();
				pcswItem.setZd020BackQuantity(hasBackQuantity.add(backQuantity));
				if(pcswItem.getZd016Price()!=null){
					pcswItem.setZd025SumMoney((pcswItem.getZd018HsQuantity().subtract(pcswItem.getZd020BackQuantity())).multiply(pcswItem.getZd016Price()));
					LogicPurchaseWareDO t= logicPurchaseWareService.findListById(pcswItem.getZd001PusWareId());
					t.setZd018TheSumMoney(t.getZd018TheSumMoney().subtract((pcswItem.getZd020BackQuantity().multiply(pcswItem.getZd016Price()))));
					logicPurchaseWareService.update(t, user);
				}
				this.logicPurchaseWareItemDAO.updateLogicPurchaseWareItemDO(pcswItem);
			}
			//其他入库
			if("GWHN".equals(zdBillType)){
				LogicWarehouseOtherinItemDO inItem = (LogicWarehouseOtherinItemDO)billMap.get(keyStr);
				if(inItem==null||StringUtils.isBlank(inItem.getId())){
					throw new SyBaseException("没有找到对应的入库单的行记录信息");
				}
				BigDecimal hasBackQuantity = inItem.getZd007BackQuantity()==null?BigDecimal.ZERO:inItem.getZd007BackQuantity();
				inItem.setZd007BackQuantity(hasBackQuantity.add(backQuantity));
				if(inItem.getZd009Price()!=null){
					inItem.setZd014SumMoney(inItem.getZd009Price().multiply((inItem.getZd007Quantity().subtract(inItem.getZd007BackQuantity()))));
					LogicWarehouseOtherinDO t= this.logicWareHouseOtherInService.findListById(inItem.getZd001OtherInId());
					t.setZd013TheSumMoney((t.getZd013TheSumMoney().subtract((inItem.getZd007BackQuantity()).multiply(inItem.getZd009Price()))));
					this.logicWareHouseOtherInService.update(t, user);
				}
				this.logicWarehouseOtherinItemDAO.updateLogicWarehouseOtherinItemDO(inItem);
			}
			//多PI入库
			if("BI".equals(zdBillType)){
				LogicWarehouseBatchInItemDO biItem=(LogicWarehouseBatchInItemDO)billMap.get(keyStr);
				if(biItem==null||StringUtils.isBlank(biItem.getId())){
					throw new SyBaseException("没有找到对应的入库单的行记录信息");
				}
				BigDecimal hasBackQuantity = biItem.getZd003BackInQuantity()==null?BigDecimal.ZERO:biItem.getZd003BackInQuantity();
				biItem.setZd003BackInQuantity(hasBackQuantity.add(backQuantity));
				if(biItem.getZd007Price()!=null){
					biItem.setZd008AllMoney(biItem.getZd007Price().multiply(biItem.getZd003InQuantity().subtract(biItem.getZd003BackInQuantity())));
					LogicWarehouseBatchInDO t=this.logicWarehouseBatchInService.findListById(biItem.getZd001BatchId());
					t.setZd008MaterialAllMoney(t.getZd008MaterialAllMoney().subtract((biItem.getZd003BackInQuantity().multiply(biItem.getZd007Price()))));
					this.logicWarehouseBatchInService.update(t, user);
				}
				this.logicWarehouseBatchInItemDAO.updateLogicWarehouseBatchInItemDO(biItem);
			}
			//其他出库
			if("GWHO".equals(zdBillType)){
				LogicWarehouseOtheroutItemDO outItem = (LogicWarehouseOtheroutItemDO)billMap.get(keyStr);
				if(outItem==null||StringUtils.isBlank(outItem.getId())){
					throw new SyBaseException("没有找到对应的入库单的行记录信息");
				}
				BigDecimal hasBackQuantity = outItem.getZd007BackQuantity()==null?BigDecimal.ZERO:outItem.getZd007BackQuantity();
				outItem.setZd007BackQuantity(hasBackQuantity.add(backQuantity));
				this.logicWarehouseOtheroutItemDAO.updateLogicWarehouseOtheroutItemDO(outItem);
			}
			//领料出库
			if("GWMO".equals(zdBillType)){
				LogicWarehouseMaterialoutItemDO outItem = (LogicWarehouseMaterialoutItemDO)billMap.get(keyStr);
				if(outItem==null||StringUtils.isBlank(outItem.getId())){
					throw new SyBaseException("没有找到对应的入库单的行记录信息");
				}
				BigDecimal hasBackQuantity = outItem.getZd007BackQuantity()==null?BigDecimal.ZERO:outItem.getZd007BackQuantity();
				outItem.setZd007BackQuantity(hasBackQuantity.add(backQuantity));
				this.logicWarehouseMaterialoutItemDAO.updateLogicWarehouseMaterialoutItemDO(outItem);
			}
			//成品入库
			if("GPSI".equals(zdBillType)){
				LogicProductionStorageinItemDO inItem = (LogicProductionStorageinItemDO)billMap.get(keyStr);
				if(inItem==null||StringUtils.isBlank(inItem.getId())){
					throw new SyBaseException("没有找到对应的入库单的行记录信息");
				}
				BigDecimal hasBackQuantity = inItem.getZd007BackQuantity()==null?BigDecimal.ZERO:inItem.getZd007BackQuantity();
				inItem.setZd007BackQuantity(hasBackQuantity.add(backQuantity));
				this.logicProductionStorageinItemDAO.updateLogicProductionStorageinItemDO(inItem);
			}
			//成品出库
			if("GPSO".equals(zdBillType)){
				LogicProductionStorageoutItemDO outItem = (LogicProductionStorageoutItemDO)billMap.get(keyStr);
				if(outItem==null||StringUtils.isBlank(outItem.getId())){
					throw new SyBaseException("没有找到对应的入库单的行记录信息");
				}
				BigDecimal hasBackQuantity = outItem.getZd007BackQuantity()==null?BigDecimal.ZERO:outItem.getZd007BackQuantity();
				outItem.setZd007BackQuantity(hasBackQuantity.add(backQuantity));
				this.logicProductionStorageoutItemDAO.updateLogicProductionStorageoutItemDO(outItem);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 修改原始单据的已退货数量
	 * @param zdBillId
	 * @param zdBillType
	 */
	private Map<String, Object> findQueryBackBillToMap(String zdBillId, String zdBillType){
		Map<String, Object> billBackMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if("PCSW".equals(zdBillType)){
			//采购入库
			billBackMap.clear();
			paramMap.clear();
			paramMap.put("pcswId", zdBillId);
			LogicPurchaseWareDO pcsw = logicPurchaseWareService.findListById(zdBillId);
			if(pcsw==null){
				throw new SyBaseException("没有查询到该采购入库单,请查证后重新提交。");
			}
			List<LogicPurchaseWareItemDO> pcswItemList = logicPurchaseWareService.findItemListByMap(paramMap);
			if(pcswItemList==null||pcswItemList.size()==0||pcswItemList.isEmpty()){
				return billBackMap;
			}else{
				for(LogicPurchaseWareItemDO pcswItem : pcswItemList){
					if(pcswItem==null||StringUtils.isBlank(pcswItem.getId())){
						continue;
					}
					String keyStr = zdBillId+"_"+pcswItem.getZd003MaterId();//采购入库
					keyStr=keyStr+"_"+(StringUtils.isEmpty(pcswItem.getZd007Model())?null:pcswItem.getZd007Model());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(pcswItem.getZd011Size())?null:pcswItem.getZd011Size());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(pcswItem.getZd009Color())?null:pcswItem.getZd009Color());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(pcswItem.getZd010Quality())?null:pcswItem.getZd010Quality());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(pcsw.getZd016PiCode())?null:pcsw.getZd016PiCode());
					billBackMap.put(keyStr, pcswItem);
				}
			}
		}
		if("GWHN".equals(zdBillType)){
			//其他入库
			billBackMap.clear();
			paramMap.clear();
			paramMap.put("gwhInId", zdBillId);
			LogicWarehouseOtherinDO inEntity = logicWareHouseOtherInService.findListById(zdBillId);
			if(inEntity==null){
				throw new SyBaseException("没有查询到该其他入库单,请查证后重新提交。");
			}
			List<LogicWarehouseOtherinItemDO> ginItemList = logicWareHouseOtherInService.findItemListByMap(paramMap);
			if(ginItemList==null||ginItemList.size()==0||ginItemList.isEmpty()){
				return billBackMap;
			}else{
				for(LogicWarehouseOtherinItemDO ginItem : ginItemList){
					if(ginItem==null||StringUtils.isBlank(ginItem.getId())){
						continue;
					}
					String keyStr = zdBillId+"_"+ginItem.getZd002MaterialsId();
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd004MaterialsModel())?null:ginItem.getZd004MaterialsModel());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd005MaterialsSize())?null:ginItem.getZd005MaterialsSize());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd005MaterialsColor())?null:ginItem.getZd005MaterialsColor());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd005MaterialsQuanlity())?null:ginItem.getZd005MaterialsQuanlity());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(inEntity.getZd005PiCode())?null:inEntity.getZd005PiCode());
					billBackMap.put(keyStr, ginItem);
				}
			}
		}
		if("BI".equals(zdBillType)){
			//多PI入库
			Map<String, String> parmMap= new HashMap<String, String>();
			parmMap.put("zd001BatchId", zdBillId);
			LogicWarehouseBatchInDO inEntity = this.logicWarehouseBatchInService.findListById(zdBillId);
			if(inEntity==null){
				throw new SyBaseException("没有查询到该其他入库单,请查证后重新提交。");
			}
			List<LogicWarehouseBatchInItemDO> ginItemList = this.logicWarehouseBatchInItemDAO.findLogicWarehouseBatchInItemByMap(parmMap);
			if(ginItemList==null||ginItemList.size()==0||ginItemList.isEmpty()){
				return billBackMap;
			}else{
				for(LogicWarehouseBatchInItemDO ginItem : ginItemList){
					if(ginItem==null||StringUtils.isBlank(ginItem.getId())){
						continue;
					}
					String keyStr = zdBillId+"_"+ginItem.getZd002MaterId();
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd002MaterModel())?null:ginItem.getZd002MaterModel());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd002MaterSpec())?null:ginItem.getZd002MaterSpec());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd002MaterColor())?null:ginItem.getZd002MaterColor());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd002MaterQuanlity())?null:ginItem.getZd002MaterQuanlity());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(ginItem.getZd005PiCode())?null:ginItem.getZd005PiCode());
					billBackMap.put(keyStr, ginItem);
				}
			}
		}
		if("GWHO".equals(zdBillType)){
			//其他出库
			billBackMap.clear();
			paramMap.clear();
			paramMap.put("gwhOutId", zdBillId);
			List<LogicWarehouseOtheroutItemDO> outItemList = logicWareHouseOtherOutService.findItemListByMap(paramMap);
			if(outItemList==null||outItemList.size()==0||outItemList.isEmpty()){
				return billBackMap;
			}else{
				for(LogicWarehouseOtheroutItemDO outItem : outItemList){
					if(outItem==null||StringUtils.isBlank(outItem.getId())){
						continue;
					}
					String keyStr = zdBillId+"_"+outItem.getZd002MaterialsId();
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd004MaterialsModel())?null:outItem.getZd004MaterialsModel());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd003MaterialsSpec())?null:outItem.getZd003MaterialsSpec());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd016MaterialsColor())?null:outItem.getZd016MaterialsColor());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd017MaterialsQuanlity())?null:outItem.getZd017MaterialsQuanlity());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd018PiCode())?null:outItem.getZd018PiCode());
					billBackMap.put(keyStr, outItem);
				}
			}
		}
		if("GWMO".equals(zdBillType)){
			//领料出库
			billBackMap.clear();
			paramMap.clear();
			paramMap.put("gwhmOutId", zdBillId);
			List<LogicWarehouseMaterialoutItemDO> materOutItemList = materialOutService.findItemListByMap(paramMap);
			if(materOutItemList==null||materOutItemList.size()==0||materOutItemList.isEmpty()){
				return billBackMap;
			}else{
				for(LogicWarehouseMaterialoutItemDO outItem : materOutItemList){
					if(outItem==null||StringUtils.isBlank(outItem.getId())){
						continue;
					}
					String keyStr = zdBillId+"_"+outItem.getZd002MaterialsId();
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd004MaterialsModel())?null:outItem.getZd004MaterialsModel());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd017MaterialsSize())?null:outItem.getZd017MaterialsSize());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd016MaterialsColor())?null:outItem.getZd016MaterialsColor());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd018MaterialsQuanlity())?null:outItem.getZd018MaterialsQuanlity());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(outItem.getZd021PiCode())?null:outItem.getZd021PiCode());
					billBackMap.put(keyStr, outItem);
				}
			}
		}
		if("GPSI".equals(zdBillType)){
			//成品入库
			billBackMap.clear();
			LogicProductionStorageinDO inEntity = logicProductionStorageInService.findListById(zdBillId);
			if(inEntity==null){
				throw new SyBaseException("没有查询到该成品入库单,请查证后重新提交。");
			}
			List<LogicProductionStorageinItemDO> gsInItemList = logicProductionStorageInService.findStorInItemList(zdBillId);
			if(gsInItemList==null||gsInItemList.size()==0||gsInItemList.isEmpty()){
				return billBackMap;
			}else{
				for(LogicProductionStorageinItemDO gsInItem : gsInItemList){
					if(gsInItem==null||StringUtils.isBlank(gsInItem.getId())){
						continue;
					}
					String keyStr = zdBillId+"_"+gsInItem.getZd002MaterialsId();
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsInItem.getZd004MaterialsModel())?null:gsInItem.getZd004MaterialsModel());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsInItem.getZd015MaterialsSize())?null:gsInItem.getZd015MaterialsSize());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsInItem.getZd016MaterialsColor())?null:gsInItem.getZd016MaterialsColor());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsInItem.getZd017MaterialsQuality())?null:gsInItem.getZd017MaterialsQuality());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(inEntity.getZd003PiCode())?null:inEntity.getZd003PiCode());
					billBackMap.put(keyStr, gsInItem);
				}
			}
		}
		if("GPSO".equals(zdBillType)){
			//成品出库
			billBackMap.clear();
			List<LogicProductionStorageoutItemDO> gsOutItemList = logicProductionStorageOutService.findStorOutItemList(zdBillId);
			if(gsOutItemList==null||gsOutItemList.size()==0||gsOutItemList.isEmpty()){
				return billBackMap;
			}else{
				String proId="";
				for(LogicProductionStorageoutItemDO gsOutItem : gsOutItemList){
					if(gsOutItem==null||StringUtils.isBlank(gsOutItem.getId())){
						continue;
					}
					proId=gsOutItem.getZd001OtherOutId();
					String keyStr = zdBillId+"_"+gsOutItem.getZd002MaterialsId();
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsOutItem.getZd004MaterialsModel())?null:gsOutItem.getZd004MaterialsModel());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsOutItem.getZd016MaterialsSize())?null:gsOutItem.getZd016MaterialsSize());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsOutItem.getZd017MaterialsColor())?null:gsOutItem.getZd017MaterialsColor());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsOutItem.getZd018MaterialsQuality())?null:gsOutItem.getZd018MaterialsQuality());
					keyStr=keyStr+"_"+(StringUtils.isEmpty(gsOutItem.getZd024Picode())?null:gsOutItem.getZd024Picode());
					billBackMap.put(keyStr, gsOutItem);
				}
				if(StringUtils.isNotBlank(proId)){//修改销售订单的状态
					LogicProductionStorageoutDO storageOut=this.logicProductionStorageOutService.findListById(proId);
					LogicSalesDO sale= new LogicSalesDO();
					sale.setZd001FormCode(storageOut.getZd002OriCode());
					List<LogicSalesDO> saleList= this.logicSalesDAO.findListByExample(sale);
					if(saleList==null||saleList.isEmpty()||saleList.size()!=1){
						throw new SyBaseException("数据错误，在修改销售订单的时候，反生错误!!!!!");
					}
					LogicSalesDO sales=saleList.get(0);
					sales.setZd025WareStatus(1);//部分出库
					this.logicSalesDAO.updateLogicSalesDO(sales);
				}
			}
		}
		return billBackMap;
	}
	
	private Map<String, BigDecimal> findHsBackQuantityToMap(String zdOriId){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("OriId", zdOriId);
		paramMap.put(BillStatusUtil.MATER_BACK_THREE, BillStatusUtil.MATER_BACK_THREE);
		List<LogicMaterialBackItemDO> backItemList = logicMaterialBackItemDAO.findListByMap(paramMap);
		if(backItemList==null||backItemList.isEmpty()||backItemList.size()==0){
			return null;
		}else{
			Map<String, BigDecimal> hasBackMap = new HashMap<String, BigDecimal>();
			for(LogicMaterialBackItemDO backItem : backItemList){
				if(backItem==null||StringUtils.isBlank(backItem.getId())){
					continue;
				}
				String keyStr = zdOriId+"_"+backItem.getZd002MaterId();
				keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterModel())?null:backItem.getZd002MaterModel());
				keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterSpec())?null:backItem.getZd002MaterSpec());
				keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterColor())?null:backItem.getZd002MaterColor());
				keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd002MaterQuanlity())?null:backItem.getZd002MaterQuanlity());
				keyStr=keyStr+"_"+(StringUtils.isEmpty(backItem.getZd012PiCode())?null:backItem.getZd012PiCode());
				BigDecimal backQuantity = backItem.getZd010TheBackQuantity()==null?BigDecimal.ZERO:backItem.getZd010TheBackQuantity();
				if(hasBackMap.get(keyStr)==null){
					hasBackMap.put(keyStr, backQuantity);
				}else{
					hasBackMap.put(keyStr, backQuantity.add(hasBackMap.get(keyStr)));
				}
			}
			return hasBackMap;
		}
	}

	@Override
	public LogicMaterialBackDO findListById(String id) throws SyBaseException {
		return logicMaterialBackDAO.findLogicMaterialBackDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return logicMaterialBackDAO.deleteBizRemoved(id);
	}

}
